﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Web;
using System.Net;
using System.Security.Cryptography;
using System.Text.RegularExpressions;

namespace vkontakte_client
{
    public class Message
    {
       public Dictionary<string, string> Fields = new Dictionary<string, string>();

      
        public static System.Collections.Generic.List<Message> XMLToMessages(string xml)
        {
            System.Collections.Generic.List<Message> dateList = new System.Collections.Generic.List<Message>();
            Regex MessageRegex = new Regex("<message>+?.+?</message>+?");
            MatchCollection MessageMatchs = MessageRegex.Matches(xml.Replace("\n", "w"));
            string[] patterns = {"from_id", "mid", "uid", "date", "read_state", "out_type", "tittle", "body", "chatId", "emoji", "first_name", "last_name"};
            foreach (Match CurrMessage in MessageMatchs)
            {
                Message TmpData = new Message();
                foreach (string pattern in patterns)
                {                    
                    string curr_pattern = "<" + pattern + ">+?.+?</" + pattern + ">+?";
                    Regex currRegex = new Regex(curr_pattern);
                    Match CurrMatch = currRegex.Match(CurrMessage.Value);
                    if (CurrMatch.Success)
                    {
                        TmpData.Fields[pattern] = (CurrMatch.Value.Replace("</"+pattern+">", "")).Replace("<"+pattern+">", "");
                    }
                }
                if (!TmpData.Fields.ContainsKey("body"))
                    TmpData.Fields["body"] = " ";
                dateList.Add(TmpData);
            }
            return dateList;
        }
        public static System.Collections.Generic.List<Message> GetHistory(string uids, int count)
        {
            
            //string xml = Http.StupidGet("messages.get", "fields=last_name,first_name&photo_sizes&out=0&count=25");
            string xml = Http.StupidGet("messages.getHistory", "uid=" + uids + "&count=" + count.ToString());
            return XMLToMessages(xml);
        }
    }
}
